function _registerEvent(target, eventType, cb) {
  if (target.addEventListener) {
    target.addEventListener(eventType, cb);
    return {
      remove: function () {
        target.removeEventListener(eventType, cb);
      },
    };
  } else {
    target.attachEvent(eventType, cb);
    return {
      remove: function () {
        target.detachEvent(eventType, cb);
      },
    };
  }
}

export function openUriWithTimeoutHack(uri, failCb, successCb) {
  let target = window;
  while (target != target.parent) {
    target = target.parent;
  }
  let handler = null;
  const timeout = setTimeout(function () {
    failCb();
    handler.remove();
  }, 1000);

  handler = _registerEvent(target, 'blur', function () {
    clearTimeout(timeout);
    handler.remove();
    successCb && successCb();
  });

  window.location = uri;
}
